import http from '../utils/request'

// 权限类型定义
export type PermissionType = 'MENU' | 'BUTTON' | 'API'
export type MethodType = 'GET' | 'POST' | 'PATCH' | 'DELETE'

// 权限接口定义
export interface Permission {
  id: number
  name: string
  code: string
  type: PermissionType
  parentId?: number
  path?: string
  redirect?: string
  icon?: string
  component?: string
  layout?: string
  keepAlive?: boolean
  method?: MethodType
  description?: string
  show?: boolean
  enable?: boolean
  order?: number
  children?: Permission[]
  roles?: any[]
}

// 创建权限DTO
export interface CreatePermissionDto {
  name: string
  code: string
  type: PermissionType
  parentId?: number
  path?: string
  redirect?: string
  icon?: string
  component?: string
  layout?: string
  keepAlive?: boolean
  method?: MethodType
  description?: string
  order?: number
  show?: boolean
  enable?: boolean
}

// 更新权限DTO
export interface UpdatePermissionDto extends Partial<CreatePermissionDto> {}

// 权限查询参数
export interface PermissionQueryParams {
  name?: string
  type?: PermissionType
  parentId?: number
  enable?: boolean
  show?: boolean
  page?: number
  num?: number
}

// 权限API
export const permissionApi = {
  // 获取所有权限（支持分页和搜索）
  getAllPermissions: (params?: PermissionQueryParams) => {
    return http.get('/permission', params)
  },

  // 获取权限树
  getPermissionTree: () => {
    return http.get('/permission/tree')
  },

  // 获取菜单树
  getMenuTree: () => {
    return http.get('/permission/menu/tree')
  },

  // 获取单个权限
  getPermission: (id: number) => {
    return http.get(`/permission/${id}`)
  },

  // 创建权限
  createPermission: (data: CreatePermissionDto) => {
    return http.post('/permission', data)
  },

  // 批量创建权限
  batchCreatePermissions: (data: CreatePermissionDto[]) => {
    return http.post('/permission/batch', data)
  },

  // 更新权限
  updatePermission: (id: number, data: UpdatePermissionDto) => {
    return http.patch(`/permission/${id}`, data)
  },

  // 删除权限
  deletePermission: (id: number) => {
    return http.delete(`/permission/${id}`)
  },

  // 获取按钮权限
  getButtonPermissions: (parentId: number) => {
    return http.get(`/permission/button/${parentId}`)
  },

  // 验证菜单路径
  validateMenuPath: (path: string) => {
    return http.get('/permission/menu/validate', { params: { path } })
  }
}

// 权限类型选项
export const PERMISSION_TYPE_OPTIONS = [
  { label: '菜单', value: 'MENU' },
  { label: '按钮', value: 'BUTTON' },
  { label: 'API', value: 'API' }
]

// HTTP方法选项
export const METHOD_TYPE_OPTIONS = [
  { label: 'GET', value: 'GET' },
  { label: 'POST', value: 'POST' },
  { label: 'PATCH', value: 'PATCH' },
  { label: 'DELETE', value: 'DELETE' }
]

// 导入图标工具
import { getIconOptions } from '../utils/iconUtils'

// 常用图标选项 - 使用Element Plus图标
export const ICON_OPTIONS = getIconOptions()
